package com.magima.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

public class ListViewUtil {
	
	private static final int DURATION_TIME = 400;
	private static double THRESHOLD = 0.1;
//	private static boolean scrolling = false;
	private static boolean scrollingFlag = false;
	private static float start,ended;
	public static void setSnappingResult(Context context,final ListView listView){
		listView.setOnScrollListener(new OnScrollListener() {
			@TargetApi(Build.VERSION_CODES.HONEYCOMB)
			public void onScrollStateChanged(final AbsListView view, int scrollState) {
				if(!scrollingFlag)
					return;
				
				switch (scrollState) {
				case OnScrollListener.SCROLL_STATE_IDLE:
						View itemView = listView.getChildAt(0); 
						int top = Math.abs(itemView.getTop()); // top is a negative value
						int bottom = Math.abs(itemView.getBottom());
						
						if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
							if(start > ended){ //向下
								if (bottom > 20){
									setSnapping(listView, view, 0);
								} else {
									setSnapping(listView, view, 0);
								}
								
							}else if(start < ended){  //向上
								if (top > 20 ){
									setSnapping(listView, view, 1);
								} else {
									setSnapping(listView, view, 0);
								}
							}else{ //未动
								setSnapping(listView, view, 0);
							}
							
						}else{
							if(start > ended){ //向下
								if (bottom > 20){
									listView.setSelectionFromTop(view.getFirstVisiblePosition(), 0);
								} else {
									listView.setSelectionFromTop(view.getFirstVisiblePosition(), 0);
								}
							}else if(start < ended){  //向上
								if (top > 20 ){
									listView.setSelectionFromTop(view.getFirstVisiblePosition()+1, 0);
								} else {
									listView.setSelectionFromTop(view.getFirstVisiblePosition(), 0);
								}
							}else{ //未动
								listView.setSelectionFromTop(view.getFirstVisiblePosition(), 0);
							}
						}
						start = 0;
						ended = 0;
						scrollingFlag = false;
//					scrolling = false;
					break;
				case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
				case OnScrollListener.SCROLL_STATE_FLING:
//					scrolling = true;
					break;
				}
				
			}
			
			@Override
			public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
				
			}
		});
		
		
		listView.setOnTouchListener(new OnTouchListener() {
			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View view, MotionEvent event) {
				scrollingFlag = true;
				switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					start = event.getY();
					break;
				case MotionEvent.ACTION_MOVE:
					if(((Math.abs(start - ended ) > THRESHOLD ) || ended == 0) && start != event.getY()){
						ended =  start == 0 ? event.getY() : start;
					}
					
					start = event.getY();
					break;
				case MotionEvent.ACTION_UP:
					
					break;
				}
				
				return false;
			}
		});
	}
	
	/**
	 * <p>ListView 滚动动画效果
	 * @param listView 
	 * @param view
	 * @param postion 1 或者 0，向上或者取当前第一个
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private static void setSnapping(final ListView listView, final AbsListView view ,final int postion){
		listView.post(new Runnable() {
			@Override
			public void run() {
				listView.smoothScrollToPositionFromTop(view.getFirstVisiblePosition() + postion, 0, DURATION_TIME);
			}
		});
	}
}
